﻿#include "pch.h"
#include "sbcs.h"

/**
 * 전역 인덱싱 순번을 얻는다.
 * @return	고유 순번.
 */
ksize_t k_nth(void)
{
	static ksize_t s_n = 1;

	return s_n++;
}


//////////////////////////////////////////////////////////////////////////
// 엔디안

/**
 * short 뒤집기 변환.
 * @param	x	kushort형 변수.
 * @return	뒤집힌 값.
 */
kushort k_htons(kushort x)
{
	return k_htobe16(x);
}

/**
 * short 뒤집기 변환.
 * @param	x	kushort형 변수.
 * @return	뒤집힌 값.
 */
kushort k_ntohs(kushort x)
{
	return k_htobe16(x);
}

/**
 * int 뒤집기 변환.
 * @param	x	kuint형 변수.
 * @return	뒤집힌 값.
 */
kuint k_htonl(kuint x)
{
	return k_htobe32(x);
}

/**
 * int 뒤집기 변환.
 * @param	x	kuint형 변수.
 * @return	뒤집힌 값.
 */
kuint k_ntohl(kuint x)
{
	return k_htobe32(x);
}


//////////////////////////////////////////////////////////////////////////
// 환경 변수

/**
 * 사용자 이름을 얻어온다.
 * @param [반환]	buf	널이 아니면 버퍼.
 * @param	size   	버퍼 크기.
 * @return	얻은 값의 길이.
 */
kint k_getusername(char* buf, ksize_t size)
{
#if _SB_WINDOWS_
	return k_getenv("USERNAME", buf, size);
#else
	return k_getenv("USER", buf, size);
#endif
}

/**
 * 컴퓨터 이름을 얻어온다.
 * @param [반환]	buf	널이 아니면 버퍼.
 * @param	size   	버퍼 크기.
 * @return	얻은 값의 길이.
 */
kint k_getcomputername(char* buf, ksize_t size)
{
#if _SB_WINDOWS_
	return k_getenv("COMPUTERNAME", buf, size);
#else
	return k_getenv("HOSTNAME", buf, size);
#endif
}

/**
 * 환경 변수를 얻는다.
 * @param	name   	변수 이름.
 * @param [반환]	buf	널이 아니면 버퍼.
 * @param	size   	버퍼 크기.
 * @return	얻은 값의 길이.
 */
kint k_getenv(const char* name, char* buf, ksize_t size)
{
#if _SB_WINDOWS_ 
#if _SB_WINDOWS_DESKTOP_
	DWORD dw = GetEnvironmentVariableA(name, buf, (DWORD)size);

	if (buf)
	{
		if (dw >= size)
			*buf = '\0';
		else
			buf[dw] = '\0';
	}

	return (kint)dw;
#else
	// 어카지.. 지원 안하는 것 같은데
	return 0;
#endif
#else
	const char* ret = getenv(name);
	ksize_t len;

	if (!ret)
		return 0;

	len = k_strlen(ret);

	if (size == 0 || !buf)
		return len;

	if (len >= size)
		*buf = '\0';
	else
		k_strcpy(buf, ret);

	return (kint)len;
#endif
}

/**
 * 환경 변수를 쓴다.
 * @param	name 	변수 이름.
 * @param	value	넣을 값.
 * @return	성공하면 참, 실패하면 거짓.
 */
kcham k_setenv(const char* name, const char* value)
{
#if _SB_WINDOWS_
#if _SB_WINDOWS_DESKTOP_
	return SetEnvironmentVariableA(name, value);	// name 널값 알아서 처리해줌
#else
	// 어카지.. 지원 안하는 것 같은데
	return 0;
#endif
#else
	char* buf;
	char* psz;
	kint l1, l2;

	k_return_value_if_fail(name != NULL, FALSE);

	l1 = k_strlen(name);
	l2 = value ? k_strlen(value) : 0;
	psz = buf = k_local_new(l1 + 1 /*=*/ + l2 + 1, char);
	psz = k_strpcpy(psz, name);
	psz = k_strpcpy(psz, "=");

	if (l2 > 0)
		k_strcpy(psz, value);

	return putenv(buf) == 0;
#endif
}


//////////////////////////////////////////////////////////////////////////
// 해시

/**
 * 포인터 해시. 일반적인 size_t 해시를 의미함.
 * @param	p	입력 변수.
 * @return	해시 값.
 */
ksize_t k_ptrhash(kpointer p)
{
#if _SB_64_
	lldiv_t t = lldiv((long long)(ksize_t)p, 127773);
	t.rem = 16807 * t.rem - 2836 * t.quot;

	if (t.rem < 0)
		t.rem += K_MAX_LONG;
#else
	ldiv_t t = ldiv((long)(ksize_t)p, 127773);
	t.rem = 16807 * t.rem - 2836 * t.quot;

	if (t.rem < 0)
		t.rem += K_MAX_INT;
#endif

	return (ksize_t)t.rem;
}

/**
 * 시간 해시. 현재 시각을 이용하여 해시값을 만든다.
 * @return	해시 값.
 */
ksize_t k_timedhash(void)
{
	static ksize_t dif = 0;

	time_t t = time(NULL);
	clock_t c = clock();

	ksize_t h1, h2;
	ksize_t i;
	ksbyte* p;

	h1 = 0;
	p = (ksbyte*)&t;

	for (i = 0; i < sizeof(time_t); i++)
	{
		h1 *= K_MAX_BYTE + 2;
		h1 += p[i];
	}

	h2 = 0;
	p = (ksbyte*)&c;

	for (i = 0; i < sizeof(clock_t); i++)
	{
		h2 *= K_MAX_BYTE + 2;
		h2 += p[i];
	}

	return (h1 + dif++) ^ h2;
}

/**
 * 콜백 해시. 함수 콜백을 해시값으로 만들어 준다.
 * @param	prime8	8비트 소수 값.
 * @param	func  	콜백 함수.
 * @param	data  	콜백 데이터.
 * @return	해시 값.
 */
ksize_t k_cbhash(kint prime8, kfunc func, kpointer data)
{
	ksize_t h;
	kany v;

	v.func = func;

#if _SB_64_
	// PP FF FF FF FD DD DD DD
	h = K_CONST_ULONG(prime8 & 0xFF) << K_CONST_ULONG(56);
	h |= (k_ptrhash(v.p) & 0xFFFFFFF) << 28;
	h |= k_ptrhash(data) & 0xFFFFFFF;
#else
	// PP FF FD DD
	h = (prime8 & 0xFF) << 24;
	h |= (k_ptrhash(v.p) & 0xFFF) << 12;
	h |= k_ptrhash(data) & 0xFFF;
#endif

	return h;
}

/**
 * 가까운 소수 얻기. 처리할 수 있는 최소 소수는 11, 최대 소수는 13845163.
 * @param	value	입력 값.
 * @return	소수 값.
 */
kuint k_primeclose(kuint value)
{
	static const kuint s_prime_table[] =
	{
		11, 19, 37, 73, 109, 163, 251, 367, 557, 823, 1237, 1861, 2777, 4177, 6247,
		9371, 14057, 21089, 31627, 47431, 71143, 106721, 160073, 240101, 360163, 540217,
		810343, 1215497, 1823231, 2734867, 4102283, 6153409, 9230113, 13845163,
	};
	kuint i;

	for (i = 0; i < K_COUNTOF(s_prime_table); i++)
		if (s_prime_table[i] > value)
			return s_prime_table[i];

	return s_prime_table[i - 1];
}

/**
 * 제곱 소수 얻기. 근거리에 해당하는 제곱 소수를 계산해준다.
 * @param	value	 	입력 값.
 * @param	min		 	최소 값.
 * @param [반환]	shift	널이 아니면 쉬프트 크기.
 * @return	소수 값.
 */
kuint k_primeshift(kuint value, kuint min, kuint* shift)
{
	static const kuint s_prime_shift_table[] =
	{
		/* 1 << 0 */
		1, 2, 3, 7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, 32749,
		/* 1 << 16 */
		65521, 131071, 262139, 524287, 1048573, 2097143, 4194301, 8388593,
		16777213, 33554393, 67108859, 134217689, 268435399, 536870909, 1073741789,
		/* 1 << 31 */
		2147483647
	};
	kuint ts;

	for (ts = 0; value; ts++)
		value >>= 1;

	ts = K_MAX(min, ts);

	if (shift) *shift = ts;

	return s_prime_shift_table[ts];
}

/// 지정한 데이터로 CRC32를 만든다.
/// @date 2013-12-22
/// @param data 자료.
/// @param size 크기.
/// @return 정수 값.
kuint k_crc32(const kbyte* data, ksize_t size)
{
	static const kuint crc32_table[] =
	{
		0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
		0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
		0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
		0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
		0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
		0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
		0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
		0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
		0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
		0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
		0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
		0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
		0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
		0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
		0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
		0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
		0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
		0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
		0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
		0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
		0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
		0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
		0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
		0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
		0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
		0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
		0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
		0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
		0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
		0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
		0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
		0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
	};
	kuint crc32 = K_MAX_UINT;
	ksize_t i;

	for (i = 0; i < size; i++)
		crc32 = crc32_table[(crc32 ^ data[i]) & 0xFF] ^ (crc32 >> 8);

	return ~crc32;
}

/// 지정한 데이터로 CRC64를 만든다.
/// @date 2013-12-22
/// @param data 자료.
/// @param size 크기.
/// @return 정수 값.
kulong k_crc64(const kbyte* data, ksize_t size)
{
	// https://github.com/srned/baselib/blob/master/crc64.c
	/* Redis uses the CRC64 variant with "Jones" coefficients and init value of 0.
	 *
	 * Specification of this CRC64 variant follows:
	 * Name: crc-64-jones
	 * Width: 64 bites
	 * Poly: 0xad93d23594c935a9
	 * Reflected In: True
	 * Xor_In: 0xffffffffffffffff
	 * Reflected_Out: True
	 * Xor_Out: 0x0
	 * Check("123456789"): 0xe9c6d914c4b8d9ca
	 *
	 * Copyright (c) 2012, Salvatore Sanfilippo <antirez at gmail dot com>
	 * All rights reserved.
	 *
	 * Redistribution and use in source and binary forms, with or without
	 * modification, are permitted provided that the following conditions are met:
	 *
	 *   * Redistributions of source code must retain the above copyright notice,
	 *     this list of conditions and the following disclaimer.
	 *   * Redistributions in binary form must reproduce the above copyright
	 *     notice, this list of conditions and the following disclaimer in the
	 *     documentation and/or other materials provided with the distribution.
	 *   * Neither the name of Redis nor the names of its contributors may be used
	 *     to endorse or promote products derived from this software without
	 *     specific prior written permission.
	 *
	 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
	 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
	 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	 * POSSIBILITY OF SUCH DAMAGE. */
	static const kulong crc64_table[] =
	{
		K_CONST_ULONG(0x0000000000000000), K_CONST_ULONG(0x7ad870c830358979),
		K_CONST_ULONG(0xf5b0e190606b12f2), K_CONST_ULONG(0x8f689158505e9b8b),
		K_CONST_ULONG(0xc038e5739841b68f), K_CONST_ULONG(0xbae095bba8743ff6),
		K_CONST_ULONG(0x358804e3f82aa47d), K_CONST_ULONG(0x4f50742bc81f2d04),
		K_CONST_ULONG(0xab28ecb46814fe75), K_CONST_ULONG(0xd1f09c7c5821770c),
		K_CONST_ULONG(0x5e980d24087fec87), K_CONST_ULONG(0x24407dec384a65fe),
		K_CONST_ULONG(0x6b1009c7f05548fa), K_CONST_ULONG(0x11c8790fc060c183),
		K_CONST_ULONG(0x9ea0e857903e5a08), K_CONST_ULONG(0xe478989fa00bd371),
		K_CONST_ULONG(0x7d08ff3b88be6f81), K_CONST_ULONG(0x07d08ff3b88be6f8),
		K_CONST_ULONG(0x88b81eabe8d57d73), K_CONST_ULONG(0xf2606e63d8e0f40a),
		K_CONST_ULONG(0xbd301a4810ffd90e), K_CONST_ULONG(0xc7e86a8020ca5077),
		K_CONST_ULONG(0x4880fbd87094cbfc), K_CONST_ULONG(0x32588b1040a14285),
		K_CONST_ULONG(0xd620138fe0aa91f4), K_CONST_ULONG(0xacf86347d09f188d),
		K_CONST_ULONG(0x2390f21f80c18306), K_CONST_ULONG(0x594882d7b0f40a7f),
		K_CONST_ULONG(0x1618f6fc78eb277b), K_CONST_ULONG(0x6cc0863448deae02),
		K_CONST_ULONG(0xe3a8176c18803589), K_CONST_ULONG(0x997067a428b5bcf0),
		K_CONST_ULONG(0xfa11fe77117cdf02), K_CONST_ULONG(0x80c98ebf2149567b),
		K_CONST_ULONG(0x0fa11fe77117cdf0), K_CONST_ULONG(0x75796f2f41224489),
		K_CONST_ULONG(0x3a291b04893d698d), K_CONST_ULONG(0x40f16bccb908e0f4),
		K_CONST_ULONG(0xcf99fa94e9567b7f), K_CONST_ULONG(0xb5418a5cd963f206),
		K_CONST_ULONG(0x513912c379682177), K_CONST_ULONG(0x2be1620b495da80e),
		K_CONST_ULONG(0xa489f35319033385), K_CONST_ULONG(0xde51839b2936bafc),
		K_CONST_ULONG(0x9101f7b0e12997f8), K_CONST_ULONG(0xebd98778d11c1e81),
		K_CONST_ULONG(0x64b116208142850a), K_CONST_ULONG(0x1e6966e8b1770c73),
		K_CONST_ULONG(0x8719014c99c2b083), K_CONST_ULONG(0xfdc17184a9f739fa),
		K_CONST_ULONG(0x72a9e0dcf9a9a271), K_CONST_ULONG(0x08719014c99c2b08),
		K_CONST_ULONG(0x4721e43f0183060c), K_CONST_ULONG(0x3df994f731b68f75),
		K_CONST_ULONG(0xb29105af61e814fe), K_CONST_ULONG(0xc849756751dd9d87),
		K_CONST_ULONG(0x2c31edf8f1d64ef6), K_CONST_ULONG(0x56e99d30c1e3c78f),
		K_CONST_ULONG(0xd9810c6891bd5c04), K_CONST_ULONG(0xa3597ca0a188d57d),
		K_CONST_ULONG(0xec09088b6997f879), K_CONST_ULONG(0x96d1784359a27100),
		K_CONST_ULONG(0x19b9e91b09fcea8b), K_CONST_ULONG(0x636199d339c963f2),
		K_CONST_ULONG(0xdf7adabd7a6e2d6f), K_CONST_ULONG(0xa5a2aa754a5ba416),
		K_CONST_ULONG(0x2aca3b2d1a053f9d), K_CONST_ULONG(0x50124be52a30b6e4),
		K_CONST_ULONG(0x1f423fcee22f9be0), K_CONST_ULONG(0x659a4f06d21a1299),
		K_CONST_ULONG(0xeaf2de5e82448912), K_CONST_ULONG(0x902aae96b271006b),
		K_CONST_ULONG(0x74523609127ad31a), K_CONST_ULONG(0x0e8a46c1224f5a63),
		K_CONST_ULONG(0x81e2d7997211c1e8), K_CONST_ULONG(0xfb3aa75142244891),
		K_CONST_ULONG(0xb46ad37a8a3b6595), K_CONST_ULONG(0xceb2a3b2ba0eecec),
		K_CONST_ULONG(0x41da32eaea507767), K_CONST_ULONG(0x3b024222da65fe1e),
		K_CONST_ULONG(0xa2722586f2d042ee), K_CONST_ULONG(0xd8aa554ec2e5cb97),
		K_CONST_ULONG(0x57c2c41692bb501c), K_CONST_ULONG(0x2d1ab4dea28ed965),
		K_CONST_ULONG(0x624ac0f56a91f461), K_CONST_ULONG(0x1892b03d5aa47d18),
		K_CONST_ULONG(0x97fa21650afae693), K_CONST_ULONG(0xed2251ad3acf6fea),
		K_CONST_ULONG(0x095ac9329ac4bc9b), K_CONST_ULONG(0x7382b9faaaf135e2),
		K_CONST_ULONG(0xfcea28a2faafae69), K_CONST_ULONG(0x8632586aca9a2710),
		K_CONST_ULONG(0xc9622c4102850a14), K_CONST_ULONG(0xb3ba5c8932b0836d),
		K_CONST_ULONG(0x3cd2cdd162ee18e6), K_CONST_ULONG(0x460abd1952db919f),
		K_CONST_ULONG(0x256b24ca6b12f26d), K_CONST_ULONG(0x5fb354025b277b14),
		K_CONST_ULONG(0xd0dbc55a0b79e09f), K_CONST_ULONG(0xaa03b5923b4c69e6),
		K_CONST_ULONG(0xe553c1b9f35344e2), K_CONST_ULONG(0x9f8bb171c366cd9b),
		K_CONST_ULONG(0x10e3202993385610), K_CONST_ULONG(0x6a3b50e1a30ddf69),
		K_CONST_ULONG(0x8e43c87e03060c18), K_CONST_ULONG(0xf49bb8b633338561),
		K_CONST_ULONG(0x7bf329ee636d1eea), K_CONST_ULONG(0x012b592653589793),
		K_CONST_ULONG(0x4e7b2d0d9b47ba97), K_CONST_ULONG(0x34a35dc5ab7233ee),
		K_CONST_ULONG(0xbbcbcc9dfb2ca865), K_CONST_ULONG(0xc113bc55cb19211c),
		K_CONST_ULONG(0x5863dbf1e3ac9dec), K_CONST_ULONG(0x22bbab39d3991495),
		K_CONST_ULONG(0xadd33a6183c78f1e), K_CONST_ULONG(0xd70b4aa9b3f20667),
		K_CONST_ULONG(0x985b3e827bed2b63), K_CONST_ULONG(0xe2834e4a4bd8a21a),
		K_CONST_ULONG(0x6debdf121b863991), K_CONST_ULONG(0x1733afda2bb3b0e8),
		K_CONST_ULONG(0xf34b37458bb86399), K_CONST_ULONG(0x8993478dbb8deae0),
		K_CONST_ULONG(0x06fbd6d5ebd3716b), K_CONST_ULONG(0x7c23a61ddbe6f812),
		K_CONST_ULONG(0x3373d23613f9d516), K_CONST_ULONG(0x49aba2fe23cc5c6f),
		K_CONST_ULONG(0xc6c333a67392c7e4), K_CONST_ULONG(0xbc1b436e43a74e9d),
		K_CONST_ULONG(0x95ac9329ac4bc9b5), K_CONST_ULONG(0xef74e3e19c7e40cc),
		K_CONST_ULONG(0x601c72b9cc20db47), K_CONST_ULONG(0x1ac40271fc15523e),
		K_CONST_ULONG(0x5594765a340a7f3a), K_CONST_ULONG(0x2f4c0692043ff643),
		K_CONST_ULONG(0xa02497ca54616dc8), K_CONST_ULONG(0xdafce7026454e4b1),
		K_CONST_ULONG(0x3e847f9dc45f37c0), K_CONST_ULONG(0x445c0f55f46abeb9),
		K_CONST_ULONG(0xcb349e0da4342532), K_CONST_ULONG(0xb1eceec59401ac4b),
		K_CONST_ULONG(0xfebc9aee5c1e814f), K_CONST_ULONG(0x8464ea266c2b0836),
		K_CONST_ULONG(0x0b0c7b7e3c7593bd), K_CONST_ULONG(0x71d40bb60c401ac4),
		K_CONST_ULONG(0xe8a46c1224f5a634), K_CONST_ULONG(0x927c1cda14c02f4d),
		K_CONST_ULONG(0x1d148d82449eb4c6), K_CONST_ULONG(0x67ccfd4a74ab3dbf),
		K_CONST_ULONG(0x289c8961bcb410bb), K_CONST_ULONG(0x5244f9a98c8199c2),
		K_CONST_ULONG(0xdd2c68f1dcdf0249), K_CONST_ULONG(0xa7f41839ecea8b30),
		K_CONST_ULONG(0x438c80a64ce15841), K_CONST_ULONG(0x3954f06e7cd4d138),
		K_CONST_ULONG(0xb63c61362c8a4ab3), K_CONST_ULONG(0xcce411fe1cbfc3ca),
		K_CONST_ULONG(0x83b465d5d4a0eece), K_CONST_ULONG(0xf96c151de49567b7),
		K_CONST_ULONG(0x76048445b4cbfc3c), K_CONST_ULONG(0x0cdcf48d84fe7545),
		K_CONST_ULONG(0x6fbd6d5ebd3716b7), K_CONST_ULONG(0x15651d968d029fce),
		K_CONST_ULONG(0x9a0d8ccedd5c0445), K_CONST_ULONG(0xe0d5fc06ed698d3c),
		K_CONST_ULONG(0xaf85882d2576a038), K_CONST_ULONG(0xd55df8e515432941),
		K_CONST_ULONG(0x5a3569bd451db2ca), K_CONST_ULONG(0x20ed197575283bb3),
		K_CONST_ULONG(0xc49581ead523e8c2), K_CONST_ULONG(0xbe4df122e51661bb),
		K_CONST_ULONG(0x3125607ab548fa30), K_CONST_ULONG(0x4bfd10b2857d7349),
		K_CONST_ULONG(0x04ad64994d625e4d), K_CONST_ULONG(0x7e7514517d57d734),
		K_CONST_ULONG(0xf11d85092d094cbf), K_CONST_ULONG(0x8bc5f5c11d3cc5c6),
		K_CONST_ULONG(0x12b5926535897936), K_CONST_ULONG(0x686de2ad05bcf04f),
		K_CONST_ULONG(0xe70573f555e26bc4), K_CONST_ULONG(0x9ddd033d65d7e2bd),
		K_CONST_ULONG(0xd28d7716adc8cfb9), K_CONST_ULONG(0xa85507de9dfd46c0),
		K_CONST_ULONG(0x273d9686cda3dd4b), K_CONST_ULONG(0x5de5e64efd965432),
		K_CONST_ULONG(0xb99d7ed15d9d8743), K_CONST_ULONG(0xc3450e196da80e3a),
		K_CONST_ULONG(0x4c2d9f413df695b1), K_CONST_ULONG(0x36f5ef890dc31cc8),
		K_CONST_ULONG(0x79a59ba2c5dc31cc), K_CONST_ULONG(0x037deb6af5e9b8b5),
		K_CONST_ULONG(0x8c157a32a5b7233e), K_CONST_ULONG(0xf6cd0afa9582aa47),
		K_CONST_ULONG(0x4ad64994d625e4da), K_CONST_ULONG(0x300e395ce6106da3),
		K_CONST_ULONG(0xbf66a804b64ef628), K_CONST_ULONG(0xc5bed8cc867b7f51),
		K_CONST_ULONG(0x8aeeace74e645255), K_CONST_ULONG(0xf036dc2f7e51db2c),
		K_CONST_ULONG(0x7f5e4d772e0f40a7), K_CONST_ULONG(0x05863dbf1e3ac9de),
		K_CONST_ULONG(0xe1fea520be311aaf), K_CONST_ULONG(0x9b26d5e88e0493d6),
		K_CONST_ULONG(0x144e44b0de5a085d), K_CONST_ULONG(0x6e963478ee6f8124),
		K_CONST_ULONG(0x21c640532670ac20), K_CONST_ULONG(0x5b1e309b16452559),
		K_CONST_ULONG(0xd476a1c3461bbed2), K_CONST_ULONG(0xaeaed10b762e37ab),
		K_CONST_ULONG(0x37deb6af5e9b8b5b), K_CONST_ULONG(0x4d06c6676eae0222),
		K_CONST_ULONG(0xc26e573f3ef099a9), K_CONST_ULONG(0xb8b627f70ec510d0),
		K_CONST_ULONG(0xf7e653dcc6da3dd4), K_CONST_ULONG(0x8d3e2314f6efb4ad),
		K_CONST_ULONG(0x0256b24ca6b12f26), K_CONST_ULONG(0x788ec2849684a65f),
		K_CONST_ULONG(0x9cf65a1b368f752e), K_CONST_ULONG(0xe62e2ad306bafc57),
		K_CONST_ULONG(0x6946bb8b56e467dc), K_CONST_ULONG(0x139ecb4366d1eea5),
		K_CONST_ULONG(0x5ccebf68aecec3a1), K_CONST_ULONG(0x2616cfa09efb4ad8),
		K_CONST_ULONG(0xa97e5ef8cea5d153), K_CONST_ULONG(0xd3a62e30fe90582a),
		K_CONST_ULONG(0xb0c7b7e3c7593bd8), K_CONST_ULONG(0xca1fc72bf76cb2a1),
		K_CONST_ULONG(0x45775673a732292a), K_CONST_ULONG(0x3faf26bb9707a053),
		K_CONST_ULONG(0x70ff52905f188d57), K_CONST_ULONG(0x0a2722586f2d042e),
		K_CONST_ULONG(0x854fb3003f739fa5), K_CONST_ULONG(0xff97c3c80f4616dc),
		K_CONST_ULONG(0x1bef5b57af4dc5ad), K_CONST_ULONG(0x61372b9f9f784cd4),
		K_CONST_ULONG(0xee5fbac7cf26d75f), K_CONST_ULONG(0x9487ca0fff135e26),
		K_CONST_ULONG(0xdbd7be24370c7322), K_CONST_ULONG(0xa10fceec0739fa5b),
		K_CONST_ULONG(0x2e675fb4576761d0), K_CONST_ULONG(0x54bf2f7c6752e8a9),
		K_CONST_ULONG(0xcdcf48d84fe75459), K_CONST_ULONG(0xb71738107fd2dd20),
		K_CONST_ULONG(0x387fa9482f8c46ab), K_CONST_ULONG(0x42a7d9801fb9cfd2),
		K_CONST_ULONG(0x0df7adabd7a6e2d6), K_CONST_ULONG(0x772fdd63e7936baf),
		K_CONST_ULONG(0xf8474c3bb7cdf024), K_CONST_ULONG(0x829f3cf387f8795d),
		K_CONST_ULONG(0x66e7a46c27f3aa2c), K_CONST_ULONG(0x1c3fd4a417c62355),
		K_CONST_ULONG(0x935745fc4798b8de), K_CONST_ULONG(0xe98f353477ad31a7),
		K_CONST_ULONG(0xa6df411fbfb21ca3), K_CONST_ULONG(0xdc0731d78f8795da),
		K_CONST_ULONG(0x536fa08fdfd90e51), K_CONST_ULONG(0x29b7d047efec8728),
	};
	kulong crc64 = K_MAX_ULONG;
	ksize_t i;

	for (i = 0; i < size; i++)
		crc64 = crc64_table[(crc64 ^ data[i]) & 0xFF] ^ (crc64 >> 8);

	return ~crc64;
}

//
ksize_t k_crchash(const kbyte* data, ksize_t size)
{
#if _SB_64_
	return k_crc64(data, size);
#else
	return k_crc32(data, size);
#endif
}


//////////////////////////////////////////////////////////////////////////
// 퀵 소트

#define _QSORT_STACK_SIZE (8 * sizeof(kpointer) - 2)

static void _k_qsort_swap(kbyte* a, kbyte* b, ksize_t stride)
{
	if (a != b)
	{
		while (stride--)
		{
			kbyte n = *a;
			*a++ = *b;
			*b++ = n;
		}
	}
}

static void _k_qsort_shortsort_context(kbyte* lo, kbyte* hi, ksize_t stride, kint(*func)(kpointer, kconstpointer, kconstpointer), kpointer context)
{
	kbyte* p, *max;

	while (hi > lo)
	{
		max = lo;

		for (p = lo + stride; p <= hi; p += stride)
		{
			if ((*func)(context, p, max) > 0)
				max = p;
		}

		_k_qsort_swap(max, hi, stride);
		hi -= stride;
	}
}

/**
 * 콘텍스트 입력 받는 퀵정렬.
 * @param	ptr				정렬할 데이터의 포인터.
 * @param	count			데이터의 갯수.
 * @param	stride			데이터의 폭.
 * @param [입력]	compfunc	비교 연산 콜백 함수.
 * @param	context			콜백 함수용 콘텍스트.
 */
void k_qsort_context(kpointer ptr, ksize_t count, ksize_t stride, kint(*compfunc)(kpointer, kconstpointer, kconstpointer), kpointer context)
{
	kbyte* lo, *hi, *mid;
	kbyte* lopos, *hipos;
	kbyte* lostk[_QSORT_STACK_SIZE], *histk[_QSORT_STACK_SIZE];
	ksize_t size;
	int stkptr;

	// 검사
	if (ptr == NULL || count == 0 || stride == 0 || compfunc == NULL)
		return;

	// 개수가 1개 이하면 안함
	if (count < 2) return;

	// 스택 초기화
	stkptr = 0;
	// 리미트 초기화
	lo = (kbyte*)ptr;
	hi = (kbyte*)ptr + stride * (count - 1);

pos_recursive:
	size = (hi - lo) / stride + 1;

	// 중간값처리를 사용해서 O(n^2) 알고리즘으로 전환
	if (size <= 8)  // 최적화된 값을 사용해야 할 것이다 -> Cut off value
		_k_qsort_shortsort_context(lo, hi, stride, compfunc, context);
	else
	{
		mid = lo + (size / 2) * stride;

		// 처음, 중간, 끝 부터 정렬 시작
		if (compfunc(context, lo, mid) > 0)
			_k_qsort_swap(lo, mid, stride);

		if (compfunc(context, lo, hi) > 0)
			_k_qsort_swap(lo, hi, stride);

		if (compfunc(context, mid, hi) > 0)
			_k_qsort_swap(mid, hi, stride);

		// 부분 정렬
		lopos = lo;
		hipos = hi;

		for (;;)
		{
			if (mid > lopos)
			{
				do
				{
					lopos += stride;
				} while (lopos < mid && compfunc(context, lopos, mid) <= 0);
			}

			if (mid <= lopos)
			{
				do
				{
					lopos += stride;
				} while (lopos <= hi && compfunc(context, lopos, mid) <= 0);
			}

			do
			{
				hipos -= stride;
			} while (hipos > mid && compfunc(context, hipos, mid) > 0);

			if (hipos < lopos)
				break;

			_k_qsort_swap(lopos, hipos, stride);

			if (mid == hipos)
				mid = lopos;
		}

		hipos += stride;

		if (mid < hipos)
		{
			do
			{
				hipos -= stride;
			} while (hipos > mid && compfunc(context, hipos, mid) == 0);
		}

		if (mid >= hipos)
		{
			do
			{
				hipos -= stride;
			} while (hipos > lo && compfunc(context, hipos, mid) == 0);
		}

		if (hipos - lo >= hi - lopos)
		{
			if (lo < hipos)
			{
				lostk[stkptr] = lo;
				histk[stkptr] = hipos;
				++stkptr;
			}

			if (lopos < hi)
			{
				lo = lopos;
				goto pos_recursive;
			}
		}
		else
		{
			if (lopos < hi)
			{
				lostk[stkptr] = lopos;
				histk[stkptr] = hi;
				++stkptr;
			}

			if (lo < hipos)
			{
				hi = hipos;
				goto pos_recursive;
			}
		}
	}

	--stkptr;

	if (stkptr >= 0)
	{
		lo = lostk[stkptr];
		hi = histk[stkptr];
		goto pos_recursive;
	}
	else
		return;
}

static void _k_qsort_shortsort(kbyte* lo, kbyte* hi, ksize_t stride, kint(*func)(kconstpointer, kconstpointer))
{
	kbyte* p, *max;

	while (hi > lo)
	{
		max = lo;

		for (p = lo + stride; p <= hi; p += stride)
		{
			if ((*func)(p, max) > 0)
				max = p;
		}

		_k_qsort_swap(max, hi, stride);
		hi -= stride;
	}
}

/**
 * 퀵정렬.
 * @param	ptr				정렬할 데이터의 포인터.
 * @param	count			데이터의 갯수.
 * @param	stride			데이터의 폭.
 * @param [입력]	compfunc	비교 연산 콜백 함수.
 */
void k_qsort(kpointer ptr, ksize_t count, ksize_t stride, kint(*compfunc)(kconstpointer, kconstpointer))
{
	kbyte* lo, *hi, *mid;
	kbyte* lopos, *hipos;
	kbyte* lostk[_QSORT_STACK_SIZE], *histk[_QSORT_STACK_SIZE];
	ksize_t size;
	int stkptr;

	// 검사
	if (ptr == NULL || count == 0 || stride == 0 || compfunc == NULL)
		return;

	// 개수가 1개 이하면 안함
	if (count < 2) return;

	// 스택 초기화
	stkptr = 0;
	// 리미트 초기화
	lo = (kbyte*)ptr;
	hi = (kbyte*)ptr + stride * (count - 1);

pos_recursive:
	size = (hi - lo) / stride + 1;

	// 중간값처리를 사용해서 O(n^2) 알고리즘으로 전환
	if (size <= 8)  // 최적화된 값을 사용해야 할 것이다 -> Cut off value
		_k_qsort_shortsort(lo, hi, stride, compfunc);
	else
	{
		mid = lo + (size / 2) * stride;

		// 처음, 중간, 끝 부터 정렬 시작
		if (compfunc(lo, mid) > 0)
			_k_qsort_swap(lo, mid, stride);

		if (compfunc(lo, hi) > 0)
			_k_qsort_swap(lo, hi, stride);

		if (compfunc(mid, hi) > 0)
			_k_qsort_swap(mid, hi, stride);

		// 부분 정렬
		lopos = lo;
		hipos = hi;

		for (;;)
		{
			if (mid > lopos)
			{
				do
				{
					lopos += stride;
				} while (lopos < mid && compfunc(lopos, mid) <= 0);
			}

			if (mid <= lopos)
			{
				do
				{
					lopos += stride;
				} while (lopos <= hi && compfunc(lopos, mid) <= 0);
			}

			do
			{
				hipos -= stride;
			} while (hipos > mid && compfunc(hipos, mid) > 0);

			if (hipos < lopos)
				break;

			_k_qsort_swap(lopos, hipos, stride);

			if (mid == hipos)
				mid = lopos;
		}

		hipos += stride;

		if (mid < hipos)
		{
			do
			{
				hipos -= stride;
			} while (hipos > mid && compfunc(hipos, mid) == 0);
		}

		if (mid >= hipos)
		{
			do
			{
				hipos -= stride;
			} while (hipos > lo && compfunc(hipos, mid) == 0);
		}

		if (hipos - lo >= hi - lopos)
		{
			if (lo < hipos)
			{
				lostk[stkptr] = lo;
				histk[stkptr] = hipos;
				++stkptr;
			}

			if (lopos < hi)
			{
				lo = lopos;
				goto pos_recursive;
			}
		}
		else
		{
			if (lopos < hi)
			{
				lostk[stkptr] = lopos;
				histk[stkptr] = hi;
				++stkptr;
			}

			if (lo < hipos)
			{
				hi = hipos;
				goto pos_recursive;
			}
		}
	}

	--stkptr;

	if (stkptr >= 0)
	{
		lo = lostk[stkptr];
		hi = histk[stkptr];
		goto pos_recursive;
	}
	else
		return;
}



